// pages/school/list/list.js
var schoolservice = require("../../../apiservice/school/schoolservice");
var type = ''; //用于判读是进入详细页面还是跳转到其他页面
var searchKey; //搜索参数
let userInfo = getApp().globalData.userInfo;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		region: [],
		schoolList: [],
		schoolType: [{
			id: 0,
			name: '全部'
		}, {
			id: 1,
			name: '幼儿园'
		}, {
			id: 2,
			name: '小学'
		}, {
			id: 3,
			name: '初中'
		}, {
			id: 4,
			name: '高中'
		}, {
			id: 5,
			name: '大学'
		}, {
			id: 6,
			name: '小学 初中'
		}, {
			id: 7,
			name: '初中 高中'
		}, {
			id: 8,
			name: '小学 初中 高中'
		}],
		customItem: "全部",
		schoolTypeIndex: 0, //记录当前类型
		opentype: "detail",
		userid: '',
		showDistrict: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		userInfo = getApp().globalData.userInfo;
		//默认用户医院的省市县
		if (userInfo && this.data.region.length === 0) {
			var region = [];
			this.setData({
				userid: userInfo.userid
			});
			region.push(userInfo.new_provincename);
			region.push(userInfo.new_cityname);
			if (userInfo.new_areaname) {
				region.push(userInfo.new_areaname);
			} else {
				region.push('');
			}
			this.setData({
				region: region
			})
		}
		//省市县赋值
		if (options.province && options.city && options.area) {
			var regionOptions = [options.province, options.city, options.area]
			console.log(regionOptions);
			this.setData({
				region: regionOptions
			})
		}
		//类型赋值
		type = options.type;
		//监听
		let eventChannel = this.getOpenerEventChannel();
		var that = this;
		if (typeof(eventChannel.on) == "function") {
			eventChannel.on('openedSchoolPage', function(e) {
				that.setData({
					opentype: e.data
				})
				type = e.data;
			})
			// eventChannel.on('schoolEvent', function(e) {
			// 	console.log(e.data);
			// 	if(e.data){
			// 		that.setData({
			// 			region: e.data
			// 		})
			// 		that.onShow();
			// 	}
			// 	
			// })
		}



	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		if (this.data.schoolTypeIndex != '0') {
			this.getSchoolListByParams();
		} else {
			//获取学校列表
			this.getSchoolByPAC();
		}
	},

	/**
	 * 生命周期回调—监听页面卸载
	 */
	onUnload: function() {

		let type = this.data.opentype;
		if (type == "choose") {
			var pages = getCurrentPages();
			var prevPage = pages[pages.length - 2]; //上一个页面

			let province = this.data.region[0];
			let city = this.data.region[1];
			let area = this.data.region[2];
			//debugger;
			//直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
			prevPage.setData({
				region: [province, city, area]
			});
		}
	},

	/**
	 * 搜索
	 * @param {*} e 
	 */
	onSearch: function(e) {
		console.log(e);
		console.log(e.detail.placeholder);
		let placeholder = e.detail.placeholder;
		searchKey = e.detail.searchKey; //搜索参数
		//debugger;
		if (searchKey || placeholder) {
			this.getSchoolListByParams();

		} else {
			wx.showToast({
				title: '请输入搜索关键字',
				icon: 'none'
			})
		}

	},
	/**
	 * 当地区切换时，获取对应地区的学校
	 * @param {*} e 
	 */
	districtTap: function(e) {
		this.setData({
			showDistrict: true
		});
	},
	bindRegionChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value);

		this.setData({
			showDistrict: false
		});
		var provinceName = e.detail.currentTarget.dataset.province;
		var cityName = e.detail.currentTarget.dataset.city;
		var areaName = e.detail.currentTarget.dataset.area;
		var region = [];
		region.push(provinceName);
		region.push(cityName);
		region.push(areaName);
		if (provinceName != '全部') {
			this.setData({
				region: region
			})
			if (!searchKey && this.data.schoolTypeIndex == "0") {
				//debugger;
				this.getSchoolByPAC();
			} else {
				this.getSchoolListByParams();
			}
		} else {
			wx.showToast({
				title: "省区不能选全部",
				icon: 'none',
				mask: true,
				duration: 2000
			});
			return;
		}



		// let region = e.detail.value;
		// if (region[0] !== '全部') {
		// 	this.setData({
		// 		region: region
		// 	})
		// 	if (!searchKey && this.data.schoolTypeIndex == "0") {
		// 		//debugger;
		// 		this.getSchoolByPAC();
		// 	} else {
		// 		this.getSchoolListByParams();
		// 	}
		// } else {
		// 	wx.showToast({
		// 		title: "省区不能选全部",
		// 		icon: 'none',
		// 		mask: true,
		// 		duration: 2000
		// 	});
		// 	return;
		// }
	},
	/**
	 * 当学校类型切换时，获取相应的学校列表
	 */
	bindSchoolTypeChange: function(e) {
		//console.log(e);

		//获取类型坐标
		this.setData({
			schoolTypeIndex: e.detail.value
		});

		this.getSchoolListByParams();

	},
	/**进入详细页面或者跳转到其他页面 */
	enterDetail: function(e) {
		var schoolId = e.currentTarget.id;
		//var type = this.data.opentype;
		//debugger;
		if (type == "detail") {
			wx.navigateTo({
				url: '/pages/school/detail/detail?schoolId=' + schoolId
			})
		} else if (type == "choose") {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.emit('schoolEvent', {
				schoolName: e.currentTarget.dataset.name,
				schoolId: e.currentTarget.id
			});

      let pages = getCurrentPages();
      let currPage = pages[pages.length - 1]; //当前页面
      let prevPage = pages[pages.length - 2]; //上一个页面

      prevPage.setData({
        school: { 
          new_schoolid: e.currentTarget.id,
          new_name: e.currentTarget.dataset.name}
      })

			wx.navigateBack({
				delta: 1
			})
			// var pages = getCurrentPages();
			// var currPage = pages[pages.length - 1]; //当前页面
			// var prevPage = pages[pages.length - 2]; //上一个页面
			// var province = this.data.region[0];
			// var city = this.data.region[1];
			// var area = this.data.region[2];
			// //直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
			// prevPage.setData({
			// 	school: {
			// 		new_schoolid: schoolId,
			// 		new_name: e.currentTarget.dataset.name,
			// 		province: province,
			// 		city: city,
			// 		area: area
			// 	}
			// });
			// wx.navigateBack({
			// 	delta: 1
			// })
		}
		// if (this.data.opentype == "choose") {
		// 	
		// }
	},
	/**
	 * 跳转到修改页面
	 * @param {*} e 
	 */
	editItem: function(e) {

		var schoolId = e.currentTarget.dataset.id;
		// console.log(schoolId);
		wx.navigateTo({
			url: '/pages/school/modify/modify?schoolId=' + schoolId + '&province=' + this.data.region[0] + "&city=" + this.data
				.region[1] + "&area=" + this.data.region[2]
		})
	},
	/**
	 * 跳转到添加页面
	 * @param {*} e 
	 */
	toAddPage: function() {
		debugger;
		let province = this.data.region[0];
		let city = this.data.region[1];
		let area = this.data.region[2];
		if (city === '全部' || area === '全部') {
			province = userInfo.new_provincename;
			city = userInfo.new_cityname;
			if (userInfo.new_areaname) {
				area = userInfo.new_areaname;
			}
		}
		wx.navigateTo({
			url: '/pages/school/add/add?province=' + province + "&city=" + city + "&area=" + area
		})
	},

	/**
	 * 获取学校信息列表
	 * @param {*} province 
	 * @param {*} city 
	 * @param {*} area 
	 */
	getSchoolByPAC: function() {
		var that = this;
		schoolservice.getSchoolByPAC(this.data.region[0], this.data.region[1], this.data.region[2]).then(function(success) {
			console.log(success);
			that.setData({
				schoolList: success.data
			});
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	getSchoolListBySearch: function() {
		
		var that = this;
		//组装查询参数
		var schoolObj = {
			new_provinceName: this.data.region[0],
			new_cityName: this.data.region[1],
			new_areaName: this.data.region[2],
			new_stage: null,
			new_name: ''
		}
		//判断搜索框是否有值
		if (searchKey != null && searchKey.length > 0) {
			schoolObj.new_name = searchKey
		}
		if (this.data.schoolTypeIndex != "0") {
			schoolObj.new_stage = this.data.schoolTypeIndex
		}
		console.log(schoolObj);
		//请求接口
		schoolservice.getSchoolListBySearch(schoolObj).then(function(success) {
			console.log(success);
			that.setData({
				schoolList: success.data
			})
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	getSchoolListByParams: function() {
		debugger;
		var that = this;
		//组装查询参数
		var schoolObj = {
			new_provinceName: this.data.region[0],
			new_cityName: this.data.region[1],
			new_areaName: this.data.region[2],
			new_hospital: userInfo.siteid,
			new_stage: null,
			new_name: ''
		}
		//判断搜索框是否有值
		if (searchKey != null && searchKey.length > 0) {
			schoolObj.new_name = searchKey
		}
		if (this.data.schoolTypeIndex != "0") {
			schoolObj.new_stage = this.data.schoolTypeIndex
		}
		console.log(schoolObj);
		//请求接口
		schoolservice.getSchoolListByParams(schoolObj).then(function(success) {
			console.log(success);
			that.setData({
				schoolList: success.data
			})
		}).catch(function(ex) {
			console.log(ex);
		})
	}
})
